package org.streets.eis.component.grid;

import org.apache.wicket.Component;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.inmethod.grid.column.AbstractColumn;


public class EditFlagColumn extends AbstractColumn {
	private static final long serialVersionUID = 1L;

	public EditFlagColumn(String columnId) {
		super(columnId, null);
		setResizable(false);
		setInitialSize(24);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Component newCell(WebMarkupContainer parent, String componentId, IModel rowModel) {
		return new BodyEditFlagPanel(componentId, rowModel);
	}

	@Override
	public Component newHeader(String componentId) {
		return new HeadEditFlagPanel(componentId);
	}

	
	private abstract class EditFlagPanel extends Panel{
		private static final long serialVersionUID = 1L;
		
		public EditFlagPanel(String id) {
			super(id);
			setRenderBodyOnly(true);
		}
		
		public EditFlagPanel(String id, final IModel<?> model) {
			super(id,model);
			setRenderBodyOnly(true);
		}
		
	}

	private class HeadEditFlagPanel extends EditFlagPanel {
		private static final long serialVersionUID = 1L;
		
		public HeadEditFlagPanel(String id){
			super(id);
			WebMarkupContainer editflag = new WebMarkupContainer("editflag") {
				private static final long serialVersionUID = 1L;

				@Override
				protected void onComponentTag(ComponentTag tag) {
					super.onComponentTag(tag);

					IModel<?> tooltipModel = getHeaderTooltipModel();
					if (tooltipModel != null) {
						Object object = tooltipModel.getObject();
						if (object != null) {
							tag.put("title", object.toString());
						}
					}					
				}
			};
			editflag.setOutputMarkupId(true);
			add(editflag);
		}		
	}
	private class BodyEditFlagPanel extends EditFlagPanel {
		private static final long serialVersionUID = 1L;

		public BodyEditFlagPanel(String id, final IModel<?> model) {
			super(id,model);
			
			WebMarkupContainer editflag = new WebMarkupContainer("editflag") {
				private static final long serialVersionUID = 1L;

				@Override
				protected void onComponentTag(ComponentTag tag) {
					super.onComponentTag(tag);

					if (getGrid().isItemEdited(model)) {
						tag.put("class", "imxt-editflag-on");
					}

					IModel<String> tooltipModel = getRowTooltipModel(model);
					if (tooltipModel != null) {
						Object object = tooltipModel.getObject();
						if (object != null) {
							tag.put("title", object.toString());
						}
					}					
				}

				@Override
				public boolean isVisible() {
					return canEdit(model);
				}
			};
			editflag.setOutputMarkupId(true);
			add(editflag);
		}
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public String getCellCssClass(IModel rowModel, int rowNum) {
		return "imxt-editflag";
	}

	@Override
	public String getHeaderCssClass() {
		return "imxt-editflag";
	}

	protected IModel<String> getRowTooltipModel(IModel<?> itemModel) {
		return Model.of("点击切换编辑状态");
	}

	protected boolean canEdit(IModel<?> model) {
		return true;
	}	
}
